<?php
namespace app\common\controller;

use think\Loader;

use think\Controller;
use think\Db;
use think\Session;

/**
 * 后台公用基础控制器
 * Class AdminBase
 * @package app\common\controller
 */
class AdminBase extends Controller
{
    protected $pageLimit = 15;
    protected $userInfo = [];

    protected function _initialize()
    {
        parent::_initialize();

        $this->checkAuth();
        $this->setUserInfo();
        $this->getMenu();

        // 输出当前请求控制器（配合后台侧边菜单选中状态）
        $this->assign('controller', Loader::parseName($this->request->controller()));
        $this->assign('action', Loader::parseName($this->request->action()));

        $this->assign('user_info', $this->userInfo);
    }

    protected function setUserInfo()
    {
        $admin_id = Session::get('admin_id');
        if ($admin_id > 0) {
            $this->userInfo = model('AdminUser')->getInfo(['id' => $admin_id], true, true);
            //放入角色ids
            $group_ids = model('AuthGroupAccess')->getUserAllGroupIds($admin_id);
            $this->userInfo['group_id'] = $group_ids;

            //放入这些角色id对应的code
            $this->userInfo['code'] = model('AuthGroup')->getCodeByIds($group_ids);
        }
    }

    /**
     * 权限检查
     * @return bool
     */
    protected function checkAuth()
    {
        if (!Session::has('admin_id')) {
            $this->redirect('admin/login/index');
        }

        $module     = $this->request->module();
        $controller = $this->request->controller();
        $action     = $this->request->action();

        $current_request = $module . '/' . $controller . '/' . $action;

        // 排除权限
        $not_check = [
            'admin/Index/index',
            'admin/Index/welcome',
            'admin/AuthGroup/getjson',
            'admin/System/clear',
            'admin/Region/get_region_by_parent_id',
        ];

        if (!in_array($current_request, $not_check)) {
            $auth     = new Auth();
            $admin_id = Session::get('admin_id');
            if (!$auth->check($current_request, $admin_id) && $admin_id != 1) {
                $this->error('没有权限');
            }
        }

        //获取当前请求的菜单名
        $auth_rule_model = model('AuthRule');
        $auth_rule_info = $auth_rule_model->where(['name' => $current_request])->find();
        $son_name = '';
        $parent_name = '';
        if (!empty($auth_rule_info)) {
            $son_name = $auth_rule_info->title;
            if ($auth_rule_info->pid > 0) {
                $parent_auth_rule_info = $auth_rule_model->where(['id' => $auth_rule_info->pid])->find();
                $parent_name = isset($parent_auth_rule_info->title) && !empty($parent_auth_rule_info->title) ? $parent_auth_rule_info->title : '';
            }
        }

        $this->assign('son_auth_rule_name', $son_name);
        $this->assign('parent_auth_rule_name', $parent_name);
    }

    /**
     * 获取侧边栏菜单
     */
    protected function getMenu()
    {
        $menu     = [];
        $admin_id = Session::get('admin_id');
        $auth     = new Auth();

        $auth_rule_list = Db::name('auth_rule')->where('status', 1)->order(['sort' => 'DESC', 'id' => 'ASC'])->select();

        foreach ($auth_rule_list as $value) {
            //放行一级菜单
            if (($value['pid'] == 0 && substr($value['name'], -7) == 'default') || $auth->check($value['name'], $admin_id) || $admin_id == 1) {
                $menu[] = $value;
            }
        }
        $menu = !empty($menu) ? array2tree($menu) : [];

        $this->assign('menu', $menu);
    }
}